﻿using System;
using System.Data.Entity.SqlServer;
using System.Data.SqlTypes;
using System.Linq;
using com.nm.db.Models.Systems;
using com.nm.library;

namespace com.nm.ApiServer.Attributes {
	/// <summary>
	/// 表示必须登录
	/// </summary>
	[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
	public class MustLoginAttribute : Attribute {

	}
	/// <summary>
	/// 表示必须是管理员
	/// </summary>
	[AttributeUsage(AttributeTargets.Method| AttributeTargets.Class)]
	public class MustAdminAttribute : Attribute {

        /// <summary>
        /// 检查当前用户是否有权限
        /// RoleType == "1" 表示是系统管理员
        /// </summary>
        /// <returns></returns>
        public static bool Check(string UserID){
            
			using (var dc = new SysEntities())
			{
			    var _userId = Convert.ToInt32(UserID);
                return (from ur in dc.sys_user_role join r in dc.sys_role on ur.RoleID 
                        equals r.ID where (r.RoleType == 1 || r.RoleType==9) && ur.UserID == _userId
                        select r).Any();
			}
		}
	}
	/// <summary>
	/// 必须是后台用户
	/// </summary>
	[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
	public class MustBackendAttribute : Attribute {
	}
}
